package top.wangjingxin.controller;

import com.alibaba.fastjson.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.wangjingxin.base.Result;
import top.wangjingxin.config.AppConfig;
import top.wangjingxin.model.article.ArticleDTO;
import top.wangjingxin.service.ArticleService;
import top.wangjingxin.util.Page;


@RequestMapping("/api/v1/article")
@RestController
public class ArticleController {
    @Autowired
    ArticleService articleService;

    @RequestMapping(value = "upload", produces = "text/html;charset=UTF-8", method = {RequestMethod.POST, RequestMethod.GET})
    public String upload(MultipartFile file, String action) {
        if ("config".equals(action)) {
            return AppConfig.config;
        }
        return (JSONArray.toJSONString(articleService.upload(file)));
    }

    @GetMapping("get")
    public Result get(int type, int subType,Page page) {
        return articleService.get(type,subType, page);
    }

    @GetMapping("getById")
    public Result getById(int id) {
        return articleService.getById(id);
    }

    @PostMapping("add")
    public Result add(@RequestBody ArticleDTO articleDTO) {
        return articleService.add(articleDTO);
    }

    @PostMapping("delete")
    public Result delete(int id) {
        return articleService.delete(id);
    }

    @PostMapping("update")
    public Result update(@RequestBody ArticleDTO articleDTO) {
        return articleService.update(articleDTO);
    }

    @GetMapping("search")
    public Result search(Page page, String key) {
        return articleService.search(page, key);
    }
    @GetMapping("config")
    public Result config(){
        return articleService.config();
    }
    @PostMapping("updateName")
    public Result updateName(String id,String name){
        return articleService.updateName(id,name);
    }
    @PostMapping("updateBelong")
    public Result updateBelong(String id,String belong){
        return articleService.updateBelong(id,belong);
    }
}
